<?php

namespace Api\Event;

/**
 * 热门视频
 * 根据播放、点赞、评论数据
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class VideoHotEvent extends BaseEvent {

    public $config;

    public function setData()
    {
        $this->config['base'] = json_decode(C('HOT_VIDEO_BASE_SCORE'), true);
        $this->config['times'] = json_decode(C('HOT_VIDEO_TIME_CONFIG'), true);

        $time_arr = array_keys($this->config['times']);
        $cur_time = time();
        $begin = $cur_time - $time_arr[count($time_arr) - 1] * 60;

        $vService = service('video');
        $cService = service('comments');
        $vhService = service('VideoHot');
        $map = [
            'create_time' => ['$gte' => $begin]
        ];

        $map['status'] = $vService::STATUS_PASS;
        $map['type']   = $vService::TYPE_NORMAL;
        $map['race_id'] = ['$eq' => null];
        $videos = $vService->where($map)->field('id,play,zan,game_id,create_time,uid')->select();

        $list = [];
        foreach ($videos as $video) {
            $minute = ($cur_time - $video['create_time']) / 60;
            $times = $this->get_times($minute);
            if (!$times) {
                continue;
            }
            $commentCount = $cService->countByVideo($video['id']);
            $statis = [
                'play'    => $video['play'] ? intval($video['play']) : 0,
                'zan'     => $video['zan'] ? intval($video['zan']) : 0,
                'comment' => $commentCount ? intval($commentCount) : 0,
            ];
            $score = $statis['play'] * $this->config['base']['play']
                   + $statis['zan'] * $this->config['base']['zan']
                   + $statis['comment'] * $this->config['base']['comment'];

            $score *= $times;

            $v = [
                'video_id' => $video['id'],
                'score'    => intval($score),
                'game_id'  => $video['game_id'],
                'create_time' => $video['create_time'],
                'uid'         => $video['uid']
            ];
            $list[] = $v;
        }
        $vhService->delete(['video_id' => ['$ne' => null]]);
        $vhService->addAll($list);

        return true;
    }

    private function get_times($minute)
    {
        foreach ($this->config['times'] as $key => $value) {
            if ($minute < $key) {
                return $value;
            }
        }
        return false;
    }

    /**
     * 查询热门视频
     * @author liubo <liubo@ilongyuan.com.cn>
     * @param  array  $game_ids
     */
    public function getData($game_ids = [], $p = 1, $pager = 10, &$total) {
        $vhService = service('VideoHot');
        $vEvent = event('video');
        $map = [];

        count($game_ids) > 0 && $map['game_id'] = ['$in' => array_map('intval', $game_ids)];
        $total = $vhService->where($map)->count();

        $videos = $vhService->where($map)->order('score desc, create_time desc')->page($p)->limit($pager)->field('video_id')->select();

        $list = [];
        foreach ($videos as $value) {
            $list[] = $value['video_id'];
        }
        return $list;
    }

    public function getRandom($game_ids = [], $start = 0, $end = 0, $pager = 10)
    {
        $map = [];
        count($game_ids) > 0 && $map['game_id'] = ['$in' => array_map('intval', $game_ids)];

        $query = service('VideoHot')->where($map)->order('score desc, create_time desc');
        if ($end > $start) {
            $query->limit($start, $end-$start);
        }
        $res = $query->getField('video_id', true);
        shuffle($res);
        return array_slice($res, 0, $pager);
    }
}